﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using SimpleRedisClinet;

namespace csharp_redis_test
{
    [TestFixture]
    public class Common : Base
    {
        [Test]
        public void SaveAndRead()
        {
            Redis redis = GetRedis();

            // save and read test
            redis.Set<string>("mykey", "我的测试 \r\n我的测试我的测试我的测试我的测");
            string rettmp = redis.Get<string>("mykey");

            Assert.IsNotNull(rettmp);
            Assert.AreEqual(0, rettmp.CompareTo("我的测试 \r\n我的测试我的测试我的测试我的测"));

            // preserve test
            redis.Set<string>("mykey", "test", true);
            rettmp = redis.Get<string>("mykey");

            Assert.AreEqual(0, rettmp.CompareTo("我的测试 \r\n我的测试我的测试我的测试我的测"));

            redis.GetDataType("mykey");

            redis.Set<int>("mykey2", 1111);
            Assert.AreEqual(1111, redis.Get<int>("mykey2"));

            rettmp = redis.GetRandomKey();
        }

        [Test]
        public void ExpireTest()
        {
            Redis redis = GetRedis();

            // clear data if exists
            redis.Del("expire_test");

            redis.Set<string>("expire_test", "expire _test");
            redis.Expire("expire_test", 1);
            Assert.IsTrue(redis.Exists("expire_test"));
            Assert.AreEqual(1, redis.GetExpireSeconds("expire_test"));
            Assert.AreEqual(-1, redis.GetExpireSeconds("expire_test_nofound"));

            System.Threading.Thread.Sleep(2000);
            Assert.IsFalse(redis.Exists("expire_test"));
        }
    }
}
